import {request} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsLeft: [],
    goodsContent: [],
    currentIndex:0,
    scrollTop:0
  },
  goodsList:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const Cates = wx.getStorageSync("cates");
    if (!Cates) {
      this.getGoods()
    }else{
      if (Date.now()-Cates.time>1000*60*10) {//10分钟缓存
        this.getGoods()
      }else{
        this.goodsList=Cates.data
        let goodsLeft = this.goodsList.map(r => r.cat_name)
        let goodsContent = this.goodsList[0].children
        this.setData({
          goodsLeft,
          goodsContent
        })
      }
    }
  },
  async getGoods(){
    let res = await request({url:'/categories'})
    this.goodsList=res
     wx.setStorageSync("cates", {time:Date.now(),data:this.goodsList});
    let goodsLeft = this.goodsList.map(r => r.cat_name)
    let goodsContent = this.goodsList[0].children
    this.setData({
      goodsLeft,
      goodsContent
    })
    /* request({
      url: '/categories'
    }).then(res => {
      this.goodsList=res
      //本地存储
      wx.setStorageSync("cates", {time:Date.now(),data:this.goodsList});
        
      let goodsLeft = this.goodsList.map(r => r.cat_name)
      let goodsContent = this.goodsList[0].children
      this.setData({
        goodsLeft,
        goodsContent
      })
    }) */
  },
  handleItemTap(e){
    //console.log(e);
    const {index} = e.currentTarget.dataset
    let goodsContent = this.goodsList[index].children
    this.setData({
      currentIndex:index,
      goodsContent,
      scrollTop:0
    })
  },
  scroll(e){
    console.log(e.detail.scrollTop)
    //console.log(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})